#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
using namespace std;

const int max_count = 10;

struct Matrix
{
	int row;
	int column;
};

struct Log
{
	int count;
	int split;
};

Log F[max_count][max_count] = {0};

int G( vector<Matrix>& matrixes, int low, int high  )
{
	if( high - low == 1 )
	{
		return 0;
	}

	int min_split = -1;
	int min_count = -1;
	for( int i = low + 1; i < high; i++ )
	{
		int count = 0;
		count += (( i - low == 1 ) ? 0 :((F[low][i].count != 0) ? F[low][i].count : G( matrixes, low, i )));
		count += ( matrixes[low].row * matrixes[i].row * matrixes[high - 1].column );
		count += (( high - i == 1 ) ? 0 :((F[i][high].count != 0) ? F[i][high].count : G( matrixes, i, high )));

		if( min_count == -1 || min_count > count )
		{
			min_count = count;
			min_split = i;
		}
	}

	F[low][high].count = min_count;
	F[low][high].split = min_split;
	return min_count;
}

void P( stringstream& s, int low, int high )
{
	if( high - low == 1 )
	{
		s << "A" << low + 1;
	}
	else
	{
		s << "(";
		P( s, low, F[low][high].split );
		s << " x ";
		P( s, F[low][high].split, high );
		s << ")";
	}
}

int main()
{
	int c = 0;
	while(true)
	{
		memset( F, 0, sizeof(F) );

		int count = 0;
		cin >> count;
		if( count == 0 ) break;

		vector<Matrix> matrixes( count );
		for( int j = 0; j < count; j++ )
		{
			Matrix& m = matrixes[j];
			cin >> m.row >> m.column;
		}
		G( matrixes, 0, matrixes.size() );
		cout << "Case " << (++c) << ": ";
		stringstream s;
		P( s, 0, matrixes.size() );
		cout << s.str();
		cout << endl;
	}
}
